//
//  Utilities.m
//  Virtual Network Manager
//
//  Created by Brandon on 4/6/12.
//  Copyright (c) 2012 PWNu8s. All rights reserved.
//

#import "Utilities.h"
#import "VMNet.h"

@implementation Utilities
+ (void)writeConfig:(NSMutableDictionary *)networks{
    NSLog(@"Writing to network");
    NSMutableString *configStr = [[NSMutableString alloc] init];
    [configStr appendString:@"VERSION=1,0"];

    NSEnumerator *enumerator = [networks keyEnumerator];
    NSString *key;
    
    while ((key = [enumerator nextObject])) {
        NSLog(@"%@",key);
        VMNet *vm = [networks objectForKey:key];
        NSLog(@"INSIDE LOOP %@",vm->vmID);
        
        [configStr appendFormat:@"\nanswer VNET_%@_DHCP_CFG_HASH 0CCF1CB25C324961C15B265EACF8395D84F9F7B2",vm->vmID];
        [configStr appendFormat:@"\nanswer VNET_%@_DHCP %@",vm->vmID,vm->dhcpEnabled];
        //[configStr appendString:vm->dhcpEnabled];
        [configStr appendFormat:@"\nanswer VNET_%@_HOSTONLY_NETMASK %@",vm->vmID,vm->dhcpSubnet];
        //[configStr appendString:vm->dhcpSubnet];
        [configStr appendFormat:@"\nanswer VNET_%@_HOSTONLY_SUBNET %@",vm->vmID,vm->dhcpNetwork];
        [configStr appendFormat:@"\nanswer VNET_%@_VIRTUAL_ADAPTER %@",vm->vmID,vm->vmEnabled];
        [configStr appendFormat:@"\nanswer VNET_%@_NAT %@",vm->vmID,vm->natEnabled];
    }    
    
    NSLog(@"\n\n%@\n\n",configStr);
    //NSError *error;
    //if(![configStr writeToFile:@"/Users/bdm7146/test.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
    //    NSLog(@"ERROR, Problem writing to file! %@\n",[error localizedFailureReason]);
    //}
}
+ (void)displayErrorPopup:(NSString *)display informativeText:(NSString *)information{    
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setInformativeText:information];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert setMessageText:display];
    [alert addButtonWithTitle:@"OK"];
    [alert runModal];
}
+ (BOOL) validateIP: (NSString *) ip {
    NSString *urlRegEx =
    @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
    NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 
    return [urlTest evaluateWithObject:ip];
}
@end
